package com.ftg.learn.day12.datastruct.thread;

import java.util.concurrent.atomic.AtomicInteger;

/**
 * @author 布衣
 */
public class Computer {

    private Integer num = 3;

    public void add() {
        synchronized (num) {
            System.out.println(num+"开始");
            while(true){
                num++;
                try {
                    Thread.sleep(1000);
                } catch (InterruptedException e) {
                    throw new RuntimeException(e);
                }
                System.out.println("add"+num);
            }
        }

    }

    public void reduce() {
        synchronized (num) {
            while(num > 0){
                num--;
                try {
                    Thread.sleep(1000);
                } catch (InterruptedException e) {
                    throw new RuntimeException(e);
                }
                System.out.println("reduce"+num);
            }
        }
    }

    public static void main(String[] args) throws InterruptedException{
        AtomicInteger a = new AtomicInteger(100);
        new Thread(()->{
            for (int i = 0; i < 1000; i++) {
                a.getAndIncrement();
            }
        });
//        Computer com = new Computer();
//
//        new Thread(com::add).start();
//
//        new Thread(com::reduce).start();

    }

}
